<?php

namespace app\admin\controller;

use think\Db;
use util\Tree;
use app\admin\model\AuthGroup as AuthGroupModel;

class AuthGroup extends Backend
{
    protected $noNeedAuth = 'get_auths';

    public function index()
    {
        if ($this->request->isAjax()) {
            $data = Db::name('auth_group')->order(['id' => 'asc'])->select();
            $data = $data ? Tree::dataToList($data) : [];
            $data = [
                'total' => count($data),
                'rows' => $data,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isGet()) {
            /* $rules = $this->auth->getRuleTree();
            $this->assign('rules',json_encode($rules,JSON_UNESCAPED_UNICODE)); */
            $groups = \app\admin\model\AuthGroup::getAllNormalGroupsTree();
            $this->assign('groups', $groups);
        }
        $pid = input('param.pid/d', 0);
        $this->assign('pid', $pid);
        return parent::add();
    }

    public function edit()
    {
        if ($this->request->isGet()) {
            $id = $this->request->param('id/d', 0);
            $group = model($this->modelName)->find($id);
            if (!$group) {
                $this->error('内容不存在');
            }

            /* $rules = $rules = $this->auth->getRuleTree(null, $group['rules']);
            $this->assign('rules',json_encode($rules,JSON_UNESCAPED_UNICODE)); */

            $groups = \app\admin\model\AuthGroup::getAllNormalGroupsTree();
            $this->assign('groups', $groups);
        } elseif ($this->request->isPost()) {
            $group_id = input('param.id/d', 0);
            $pgroup_id = input('param.pid/d', 0);
            if ($group_id) {
                $children = AuthGroupModel::getAllNormalGroupsByPid($group_id);
                $children_ids = Tree::getChildrenIds($children, $group_id);
                if (!empty($children_ids) && in_array($pgroup_id, $children_ids)) {
                    $this->error('父级组别不能是它的子组别或本身');
                }
    
                $rules  = Db::name('auth_group')->where('id', $group_id)->value('rules');
                $rules = $rules ? explode(',', $rules) : [];
            }
            if ($group_id != 0 && $group_id == $pgroup_id) {
                $this->error('父级不能是自己');
            }
        }
        return parent::edit();
    }

    /**
     * 根据用户组获取权限
     *
     * @return void
     */
    public function get_auths()
    {
        $group_id = input('param.group_id/d', 0);
        $pgroup_id = input('param.pgroup_id/d', 0);
        $rules = [];
        if ($group_id) {
            $children = AuthGroupModel::getAllNormalGroupsByPid($group_id);
            $children_ids = Tree::getChildrenIds($children, $group_id);
            if (!empty($children_ids) && in_array($pgroup_id, $children_ids)) {
                $this->error('父级组别不能是它的子组别或本身');
            }

            $rules  = Db::name('auth_group')->where('id', $group_id)->value('rules');
            $rules = $rules ? explode(',', $rules) : [];
        }
        if ($group_id != 0 && $group_id == $pgroup_id) {
            $this->error('父级不能是自己');
        }


        $auths = AuthGroupModel::getAuthListByGroupId($pgroup_id, $rules);
        $this->success('ok', '', ['auths' => $auths]);
    }
}
